package com.alibaba.citrus.ots.platform.enhance.functions.diff.activity.router;

import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.platform.enhance.facade.service.diff.common.DiffCheckRequest;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.constant.DiffStrategyRouteDict;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.constant.SalesOrderDiffStandardFieldDict;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.router.DiffCheckProcessor;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.template.OneDiffOneTemplate;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.common.FieldLineDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByIds2Request;
import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByIds2Response;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.bcots.service.UnifiedInventoryService;
import com.epoch.app.otspricecenter.enums.DimensionPropertyEnum;
//import com.epoch.app.otspricecenter.propertyprocess.dto.ObtainInnerCodeAndOuterCodeMapRequest;
import com.epoch.app.otspricecenter.propertyprocess.dto.ObtainInnerCodeAndOuterCodeMapRequest;
import com.epoch.app.otspricecenter.propertyprocess.service.PropertyProcessService;
import com.google.common.collect.Lists;
import com.ql.util.express.ExpressRunner;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author: qiaojiange
 * @create: 2022/01/19
 * @Description sap015主单（基准）和 销售主单 之间的对账
 */
@Component
public class Sap015SDODiffSalesOrderProcessor extends DiffCheckProcessor {

    private Log log = Log.getLogger(Sap015SDODiffSalesOrderProcessor.class);


    @Resource
    private OneDiffOneTemplate oneDiffOneTemplate;


    @Resource
    private PropertyProcessService propertyProcessService;


    @Resource
    private BaseDataService baseDataService;

    @Resource
    private UnifiedInventoryService unifiedInventoryService;

    @Override
    public String obtainStrategyRoute() {
        return DiffStrategyRouteDict.SAP015SDO_DIFF_SALES_ORDER.getStrategyCode();
    }

    @Override
    public Result<String> diff(DiffCheckRequest diffCheckRequest) {

        //sap015和销售主单对账，需要处理数据，比如货品的内外码转换，金额*100的问题
        //这里单独的使用每一个字段进行对账
        Map<String, FieldLineDTO> baseLineDataMap = diffCheckRequest.getBaseLineDataMap();
        Map<String, FieldLineDTO> salesOrderDiffDataMap = diffCheckRequest.getDiffDataMap();

        //字段处理
        detailDBFieldValue(salesOrderDiffDataMap);

        return super.diff(diffCheckRequest, oneDiffOneTemplate);
    }


    public void detailDBFieldValue(Map<String, FieldLineDTO> dto) {

        //销售组织处理
        FieldLineDTO saleOrganizationCodeDTO = dto.get(SalesOrderDiffStandardFieldDict
                .SALE_ORGANIZATION_CODE.getFieldCode());
        String saleOrganizationCodeValue = saleOrganizationCodeDTO.getFieldValue();
        if (StringUtils.isNotBlank(saleOrganizationCodeValue)) {
            ObtainInnerCodeAndOuterCodeMapRequest organizationRequest = new ObtainInnerCodeAndOuterCodeMapRequest();
            organizationRequest.setCodeList(Lists.newArrayList(saleOrganizationCodeValue));
            organizationRequest.setPropertyCode(DimensionPropertyEnum.ORG_SALES_ORGANIZATION_CODE.getKey());
            organizationRequest.setInnerCode(true);

            Map<String, String> organizationMap = (Map<String, String>) propertyProcessService.obtainInnerCodeAndOuterCodeMap(organizationRequest);
            saleOrganizationCodeDTO = FieldLineDTO.of("organizationCode", SalesOrderDiffStandardFieldDict.SALE_ORGANIZATION_CODE.getFieldCode(), organizationMap.get(saleOrganizationCodeValue));
            dto.put(SalesOrderDiffStandardFieldDict.SALE_ORGANIZATION_CODE.getFieldCode(), saleOrganizationCodeDTO);
        }
        //销售渠道处理
        FieldLineDTO saleChannelCodeDTO = dto.get(SalesOrderDiffStandardFieldDict
                .SALE_CHANNEL_CODE.getFieldCode());
        String saleChannelCodeValue = saleChannelCodeDTO.getFieldValue();
        if (StringUtils.isNotBlank(saleChannelCodeValue) && StringUtils.isNotBlank(saleChannelCodeValue.trim())) {
            BaselineGetOrganizationByIds2Request request = new BaselineGetOrganizationByIds2Request();

            request.setIds(Lists.newArrayList(saleChannelCodeValue));
            BaselineGetOrganizationByIds2Response response = baseDataService.baselineGetOrganizationByIds2(request);
            if (Objects.nonNull(response) && CollectionUtils.isNotEmpty(response.getResult())) {
                saleChannelCodeDTO = FieldLineDTO.of("saleChannelCode", SalesOrderDiffStandardFieldDict
                        .SALE_CHANNEL_CODE.getFieldCode(), response.getResult().get(0).getOutCode());
                dto.put(SalesOrderDiffStandardFieldDict.SALE_CHANNEL_CODE.getFieldCode(), saleChannelCodeDTO);
            }


        }

        //销售部门处理
        FieldLineDTO saleDepartmentCodeDTO = dto.get(SalesOrderDiffStandardFieldDict
                .SALE_DEPARTMENT_CODE.getFieldCode());
        String saleDepartmentCodeValue = saleDepartmentCodeDTO.getFieldValue();
        if (StringUtils.isNotBlank(saleDepartmentCodeValue) && StringUtils.isNotBlank(saleDepartmentCodeValue.trim())) {
            BaselineGetOrganizationByIds2Request request = new BaselineGetOrganizationByIds2Request();

            request.setIds(Lists.newArrayList(saleDepartmentCodeValue));
            BaselineGetOrganizationByIds2Response response = baseDataService.baselineGetOrganizationByIds2(request);
            if (Objects.nonNull(response) && CollectionUtils.isNotEmpty(response.getResult())) {

                saleDepartmentCodeDTO = FieldLineDTO.of("departmentCode", SalesOrderDiffStandardFieldDict
                        .SALE_DEPARTMENT_CODE.getFieldCode(), response.getResult().get(0).getOutCode());
                dto.put(SalesOrderDiffStandardFieldDict.SALE_DEPARTMENT_CODE.getFieldCode(), saleDepartmentCodeDTO);
            }

        }

        //处理发货仓库编码(该数据在detail里面,并且需要转换)


    }
}
